package org.zhxy.zhxy.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zhxy.zhxy.utils.Result;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 自定义全局异常处理
 * @version: v1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 记录异常日志
     * @param req
     * @param message
     * @param e
     */
    private void logError(HttpServletRequest req, String message, Exception e) {
        log.error("{}! 请求URL: {}, 原因: {}", message, req.getRequestURL(), e.getMessage());
    }

    /**
     * 处理空指针异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResponseEntity<Result> handleNullPointerException(HttpServletRequest req, NullPointerException e) {
        logError(req, "发生空指针异常", e);
        Result result = Result.fail(ExceptionEnum.INTERNAL_SERVER_ERROR.getCode(), ExceptionEnum.INTERNAL_SERVER_ERROR.getMsg());
        return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理请求数据格式不符异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public ResponseEntity<Result> handleIllegalArgumentException(HttpServletRequest req, IllegalArgumentException e) {
        logError(req, "请求的数据格式不符", e);
        Result result = Result.fail(ExceptionEnum.BODY_NOT_MATCH.getCode(), ExceptionEnum.BODY_NOT_MATCH.getMsg());
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }

    /**
     * 处理未授权异常（Token 不匹配）
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseBody
    public ResponseEntity<Result> handleTokenException(HttpServletRequest req, UnauthorizedException e) {
        logError(req, "Token 不匹配", e);
        Result result = Result.fail(ExceptionEnum.UNAUTHORIZED.getCode(), ExceptionEnum.UNAUTHORIZED.getMsg());
        return new ResponseEntity<>(result, HttpStatus.UNAUTHORIZED);
    }


    @ExceptionHandler(value = WxNotBoundException.class)
    @ResponseBody
    public ResponseEntity<Result> WxNotBoundException(HttpServletRequest req, WxNotBoundException e) {
        logError(req, "微信未绑定", e);
        Result result = Result.fail(ExceptionEnum.WX_NOT_BOUND.getCode(), ExceptionEnum.WX_NOT_BOUND.getMsg());
        return new ResponseEntity<>(result, HttpStatus.BAD_GATEWAY);
    }

    /**
     * 处理其他未知异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity<Result> handleException(HttpServletRequest req, Exception e) {
        logError(req, "未知异常", e);
        Result result = Result.fail(ExceptionEnum.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
        return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
